package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Description 页面跳转类
 * @Author xuegang
 * @Date 2021-02-02 上午12:44
 */
@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;

    /**
     * 首页页面进行跳转
     *
     * @param model 首页页面视图
     * @return
     */
    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        // TODO 1、查出所有的1级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();

        // 视图解析器进行拼串：
        // classpath:/templates/ + 返回值 +  .html
        model.addAttribute("categorys", categoryEntities);
        return "index";
    }

    /**
     * 首页三级分类查询，调用后端接口（数据库的数据）
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatalogJson() {
        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }
}
